/*************************************************************************
	> File Name: P1616.cpp
	> Author: yufangze
	> Mail: 2224590123@qq.com
	> Created Time: 2025-01-09 14:17:13
 ************************************************************************/

#include <iostream>
#include <set>
#include <map>
#include <vector>
#include <algorithm>
using namespace std;

#define MAX_N 10000000
long long dp[MAX_N + 5];

int main() {
    int m, t;
    cin >> t >> m;
    for (int i = 1, c, val; i <= m; i++) {
        scanf("%d%d", &c, &val);
        for (int j = c; j <= t; j++) {
            dp[j] = max(dp[j], dp[j - c] + val);
        }
    }
    cout << dp[t] << endl;
    return 0;
}
